﻿Imports System.Data.OleDb

Public Class HauntedLocationsInfo
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then Return
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("HauntedLocationString").ConnectionString)

        'meaning, if it is a post back.then we jump out of the function and don not run the rest below
        Dim sql = "INSERT INTO LocationsViewedByUsers (Location_fkid,InvestigatorName) VALUES (@f1,@f2)"
        Dim cmd = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters().AddWithValue("@f1", Request.Params("ID"))
        cmd.Parameters().AddWithValue("@f2", User.Identity.Name)
        cmd.ExecuteNonQuery()
        conn.Close()

    End Sub

    Protected Sub btn_comment_Click(sender As Object, e As EventArgs) Handles btn_comment.Click
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("HauntedLocationString").ConnectionString)

        Dim sql = "INSERT INTO Comments (Author, Subject, Comment, HauntedLocations_fkid) Values (@f1,@f2,@f3,@f4)"
        Dim cmd = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_Subject.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Comment.Text)
        cmd.Parameters.AddWithValue("@f4", Request.Params("ID"))
        cmd.ExecuteNonQuery()
        conn.Close()

        Response.Redirect("MoreDetails.aspx/?ID=" & Request.Params("ID"))

    End Sub

    Protected Sub btn_tag_Click(sender As Object, e As EventArgs) Handles btn_tag.Click
        Dim locationID = Request.Params("ID")
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("HauntedLocationString").ConnectionString)
        
        conn.Open()

        Dim newtags = tb_newtag.Text.Split(",")
        For Each newtag As String In newtags
            Dim isTagThereCmd As New OleDbCommand("SELECT * FROM TAGS WHERE Tag=@f1", conn)
            isTagThereCmd.Parameters.AddWithValue("@f1", newtag)
            Dim dr = isTagThereCmd.ExecuteReader()
            If Not dr.HasRows Then
                Dim addTagCmd As New OleDbCommand("INSERT INTO Tags (Tag) VALUES (@f1)", conn)
                addTagCmd.Parameters.AddWithValue("@f1", newtag)
                addTagCmd.ExecuteNonQuery()

            End If
            Dim findNewTagIDCmd As New OleDbCommand("SELECT ID From Tags WHERE Tag=@f1", conn)
            findNewTagIDCmd.Parameters.AddWithValue("@f1", newtag)
            Dim newTagDr = findNewTagIDCmd.ExecuteReader()
            newTagDr.Read()
            Dim newID = newTagDr.Item("ID")
            Dim makeAssociationCmd As New OleDbCommand("INSERT INTO TagLocationJunction (Tag_fkid, Location_fkid) VALUES (@f1,@f2)", conn)
            makeAssociationCmd.Parameters.AddWithValue("@f1", newID)
            makeAssociationCmd.Parameters.AddWithValue("@f2", locationID)
            makeAssociationCmd.ExecuteNonQuery()
        Next






        For Each li As ListItem In Tags.Items
            If (li.Selected = True) Then
                Dim sql = "INSERT INTO TagLocationJunction (Tag_fkid, Location_fkid) VALUES (@f1, @f2) "
                Dim cmd As New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@f1", li.Value)
                cmd.Parameters.AddWithValue("@f2", locationID)
                cmd.ExecuteNonQuery()

            End If
        Next
        conn.Close()
    End Sub
End Class